<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="from"%>
<html>
<head>
<meta charset="UTF-8">
<title><spring:message code="design_platform_name"/></title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/style/icon.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/resources/easyui/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/resources/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/resources/style/main.css">

    <script type="text/javascript">

var schemaTreeData = <%=request.getAttribute("schemaData") %>;
var databaseId;
$(function(){
	$('#help_tree').tree({
		checkbox: false,
		animate:true,
		lines:true,
		data: schemaTreeData,
		onClick:function(node) {
			var tree=$(this).tree;
			if (node.attributes == "<spring:message code="schema_database_type"/>") {
                databaseId=node.id;
				$('#show_win').panel('refresh','<%=request.getContextPath() %>/page/field');
				$('body').layout('panel', 'center').panel('setTitle', node.text);
			}
		},
		formatter: function(node) {
			return node.text + "-";
		}
	});
	$('#show_win').panel({
				fit:true,
				border:false,
				noheader:false
	});
});

</script>
</head>
<body class="easyui-layout">
<div data-options="region:'north'" style="height:65px;overflow:hidden;">
 <!--   <table style="background:url('<%=request.getContextPath() %>/resources/images/utitle.png');
    background-position: center center;  background-repeat: repeat;"
    width=100% border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td width="2%"></td>
            <td width=5% valign="center" ><img src="<%=request.getContextPath() %>/resources/images/ulogo.png" width="40" height="40" /></td>
            <td width=73% valign="top" align="left">
                <div align="justify" >
                    <p style="font-size:24px;font-family:黑体; font-weight: bold"><spring:message code="design_platform_name"/></p>
                </div></td>
            <td width=20% valign="middle" class="STYLE4">
                <div align="justify" >
                    <TextBox style="font-weight: bold; font-size: 14px; font-family: 黑体">
                        <spring:message code="welcome"/>${currentUser.userName}
                    </TextBox>
                    <a  href="<%=request.getContextPath() %>/logout"><img src="resources/images/exit.png" width="17" height="17"/></a>
                </div>
            </td>
        </tr>
        </table>//-->
            <table border="0" cellspacing="0" cellpadding="0" style="width:100%;">
        <tbody><tr>
            <td width="170" align="left"><div style="width:100%; height:69px; BACKGROUND-IMAGE: url(resources/images/ulogo.png);opacity:0.4;BACKGROUND-POSITION: left center; BACKGROUND-REPEAT: no-repeat;"></div></td>
            <td width="490" align="left"><div style="width:100%; height:69px; BACKGROUND-IMAGE: url(resources/images/schema-design-top.png);BACKGROUND-POSITION: left center; BACKGROUND-REPEAT: no-repeat;"></div></td>
            <td align="right">
                <div style="text-align:right; width:100%; height:68px; BACKGROUND-IMAGE: url(resources/images/topright.png);BACKGROUND-POSITION: right center; BACKGROUND-REPEAT: no-repeat; float: right">
                    <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
                        <tbody><tr>
                            <td id="task1" rowspan="1" valign="bottom">&nbsp;</td>
                            <td width="150"><TextBox style="font-weight: bold; font-size: 14px; font-family: 黑体"><spring:message code="case_design_welcome"/>${currentUser.userName}</TextBox></td>
                            <td width="55"><a  href="<%=request.getContextPath() %>/logout"><img src="resources/images/exit.png" width="17" height="17"/></a>
                            </td>
                            <td width="55">&nbsp;
                            </td>
                        </tr>
                    </tbody></table>
                </div>
            </td>
        </tr>
    </tbody></table>
</div>

<div class="easyui-panel" data-options="region:'west',split:true,title:'<spring:message code="accordion_title"/>',iconCls:'icon-help'" style="width:320px;padding:5px; text-align:left;">
        <div id="rolelist_dg_toolbar" style="position: fixed;top: 93px;background: white;height: 22px;width: 310px;z-index:10;">
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newSchema()"><spring:message code="schema_node_add"/></a>
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editSchema()"><spring:message code="schema_node_edit"/></a>
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="deleteSchema()"><spring:message code="schema_node_delete"/></a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="dataUp()"><spring:message code="case_design_up"/></a>
        </div>
        <div style="position: relative;top:22px;z-index: 9">
            <ul id="help_tree" class="easyui-tree"></ul>
        </div>
        <div id="catalog_save_dialog" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
             closed="true" buttons="#catalog_save_dialog_buttons" modal="true">
            <div class="ftitle"><spring:message code="input_node_info"/> </div>
            <div id="catalog_info_form">
                <div class="fitem">
                    <label><spring:message code="node_name"/> </label>
                    <input type="text" name="nodeName" required="true"/>
                </div>
                <div id="parentListType0" class="fitem">
                    <label><spring:message code="node_type"/></label>
                    <input name="schema_type_radio" type="radio" value="<spring:message code="schema_database_type"/>" /><label><spring:message code="database"/></label>
                    <input name="schema_type_radio" type="radio" value="<spring:message code="schema_catalog_type"/>" /><label><spring:message code="schema_catalog"/></label>
                </div>
            </div>
            <div id="#catalog_save_dialog_buttons">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveSchema()"><spring:message code="save"/> </a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#catalog_save_dialog').dialog('close')"><spring:message code="cancel"/> </a>
            </div>
        </div>
</div>
<div data-options="region:'center'" title="<spring:message code='main_win_name'/>" style=" padding:10px; text-align:left;">
  <div id="show_win" class="side_win"></div>
</div>

<script type="text/javascript">
var a=0;
function dataUp(){
	if(a==0){
		//收起
	$('#help_tree').tree("collapseAll");
	a+=1;
	}else{
		//展开
		 $('#help_tree').tree("expandAll");
		 a-=1;
	}
}
setInterval(function() {
	var url = '<%=request.getContextPath() %>/checkSession';
	$.get(url, function(result) {
		if (! result.successful) {
			window.location.href="<%=request.getContextPath() %>";
		}
	}, 'json');
}, 60000);
</script>
<script type="text/javascript">
    var schemaUrl={
        fieldLoad:"<%=request.getContextPath()%>/field/loadField",
        schemaAdd:'<%=request.getContextPath() %>/schema/create',
        schemaUpdate:'<%=request.getContextPath() %>/schema/update',
        schemaDelete:'<%=request.getContextPath() %>/schema/delete',
        userList:'<%=request.getContextPath() %>/user/list',
        getSchemaRemark3:"<%=request.getContextPath()%>/schema/getSchemaRemark3",
        getSchemaInfo:"<%=request.getContextPath()%>/schema/getSchemaInfo",
        setSchemaRemark3:"<%=request.getContextPath()%>/schema/setSchemaRemark3",
        savePicture:"<%=request.getContextPath()%>/schema/savePicture",
        loadAuthUsers:'<%=request.getContextPath() %>/schema/loadAuthUsers',
        loadReadUsers:'<%=request.getContextPath() %>/schema/loadReadUsers',
        loadWriteUsers:'<%=request.getContextPath() %>/schema/loadWriteUsers',
        saveAuthUsers:'<%=request.getContextPath() %>/schema/saveAuthUsers'
    };
    var url;

    //记录当前选中的节点id
    var schemaId;
    var schemaType;
    var schemaName;
    $('#help_tree').tree({
        onSelect: function (node) {
            schemaId = node.id;
            schemaType =parseInt( node.attributes);
            schemaName = node.text;            
        }
    });

    function newSchema(){
        var node=$('#help_tree').tree('getSelected');
        if(node ||isTreeEmpty()){
            var listType =parseInt(node.attributes);
            switch (listType){
                case 2:$.messager.alert("<spring:message code="operation_tips"/>","<spring:message code="tips_one"/>","warning");
                    return;
            }
            $('#catalog_save_dialog').dialog('open').dialog('setTitle',"<spring:message code="add_node_title"/>");
            $('#catalog_info_form').form('clear');
            //默认是选择数据库
            $("input[name='schema_type_radio']").eq(0).attr("checked","checked");
            $('#parentListType0').show();
            url=schemaUrl.schemaAdd;
        }else{
            $.messager.alert("<spring:message code="operation_tips"/>","<spring:message code="tips_two"/>","warning");
        }
    }

    function isTreeEmpty(){
        var root = $('#help_tree').tree('getRoot');
        return root == null;
    }

    function editSchema(){
        var node=$('#help_tree').tree('getSelected');
        if(node){
            $("input[name='nodeName']").prop('value',node.text);
            $('#parentListType0').hide();
            $('#catalog_save_dialog').dialog('open').dialog('setTitle','编辑节点');
            url=schemaUrl.schemaUpdate;
        }else{
            if(!isTreeEmpty())
                $.messager.alert("操作提示","请选择节点","warning");
            $.messager.alert("操作提示","请添加节点","warning");
        }
    }

    function saveSchema(){
        var node=$('#help_tree').tree('getSelected');

        var name=$("input[name='nodeName']")[0].value;
        var pid;
        //空树 创建根节点
        if(node == null)
            pid = 0;
        else
            pid = node.id;
        if(url==schemaUrl.schemaAdd) {
            //var type = $(".fitem[type='category'][style!='display: none;'] .combo-value")[0].value;
            var type=$("#parentListType0 input[name='schema_type_radio']:checked").val();
            type=parseInt(type);
        }
        var data={name:name,parentId:pid,type:type};
        if(url == schemaUrl.schemaUpdate) {
            data.id = node.id;
        }
        $.get(url,data,
                function(result){
                    result = eval(result);
                    if(result.successful){
                        $('#catalog_save_dialog').dialog('close');
                        if(url==schemaUrl.schemaAdd) {
                            var nodeData = {id: result.data, text: data.name, attributes: type, parentId: data.id};
                            if (type == "<spring:message code="schema_catalog_type"/>") {
                                nodeData.iconCls = "<spring:message code="schema_catalog_icon"/>";
                            } else if (type == "<spring:message code="schema_database_type"/>"){
                                nodeData.iconCls = "<spring:message code="schema_database_icon"/>";
                            }
                            $("#help_tree").tree("append",{
                                parent:node.target,
                                data:[nodeData]
                            });
                        }else if(url == schemaUrl.schemaUpdate){
                            node.text=data.name;
                            $("#help_tree").tree("update",node);
                        }
                    }
                    $.messager.show({title:'操作结果',msg:result.message});

                });
    }

    function deleteSchema(){
        var node=$('#help_tree').tree('getSelected');
        var pid=node.parentId;
        if(node){
             $.messager.confirm('Confirm', '<spring:message code="tips_delete"/>', function (n) {
                    if (n) {
                        $.post(schemaUrl.schemaDelete, {id: node.id}, function (result) {
                            if(result.successful) {
                                $("#help_tree").tree("remove", node.target);
                                if (node.attributes ==<spring:message code="schema_database_type"/>) {
                                    databaseId = 0;
                                    $('#show_win').panel('refresh', '<%=request.getContextPath() %>/page/field');
                                }
                            }
                            $.messager.show({title: '操作结果', msg: result.message});
                        })
                    }
                });
        }else{
            if(!isTreeEmpty())
                $.messager.alert("<spring:message code="operation_tips"/>","<spring:message code="tips_two"/> ","warning");
            $.messager.alert("<spring:message code="operation_tips"/>","<spring:message code="tips_one"/> ","warning");
        }
    }
</script>
<script type="text/javascript" src="<%=request.getContextPath() %>/resources/js/field-design.js">
</script>
</body>
</html>
